//
//  SSPoisonWordPanelController.m
//  Investigation Catalyst
//
//  Created by Tobias Haughton on Tue Jun 08 2004.
//  Copyright (c) 2004 Starline Software Ltd. All rights reserved.
//

#import "SSPoisonWordPanelController.h"
#import "SSCatalystDocument.h"

@implementation SSPoisonWordPanelController

+ (id)sharedPoisonWordController {
   static SSPoisonWordPanelController *sharedPoisonWordController = nil;

   if(!sharedPoisonWordController) {
      sharedPoisonWordController = [[SSPoisonWordPanelController allocWithZone:[self zone]] init];
   }
   return sharedPoisonWordController;
}

- (id)init {
   self = [super initWithWindowNibName:@"PoisonWordPanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"PoisonWordPanel"];
   }
   return self;
}

- (void)awakeFromNib
{
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(setNeedsUpdate)
                                                name:@"SSPoisonWordsChanged"
                                              object:nil];

   NSButtonCell *buttonCell = [[[NSButtonCell alloc] initTextCell: @""] autorelease];
   [buttonCell setEditable: YES];
   [buttonCell setButtonType: NSSwitchButton];
   [[wordTable tableColumnWithIdentifier:@"isDefaultValue"] setDataCell:buttonCell];
}

- (IBAction)addWord:(id)sender
{
   //create a new poison word
   SSPoisonWord *poisonWord = [[SSPoisonWord alloc] initWithPoisonWord:@"New Poison Word" warningMessage:@"This word should be avoided if possible"];

   //add to current document
   [[[NSDocumentController sharedDocumentController] currentDocument] addPoisonWord:poisonWord];
}

- (IBAction)removeWord:(id)sender
{
   SSPoisonWord *poisonWord = [[[[NSDocumentController sharedDocumentController] currentDocument] poisonWordArray] objectAtIndex:[wordTable selectedRow]];
   [[[NSDocumentController sharedDocumentController] currentDocument] removePoisonWord:poisonWord];
}

- (void)updateDataArray
{
   [dataArray release];
   dataArray = [[NSMutableArray arrayWithArray:[[[NSDocumentController sharedDocumentController] currentDocument] poisonWordArray]] retain];
   [wordTable reloadData];
}

- (void)setNeedsUpdate
{
   needsUpdate = YES;
}
//*************************
- (void)setMainWindow:(NSWindow *)mainWindow {
   needsUpdate = YES;
}

- (void)windowDidUpdate:(NSNotification *)notification {
   //this is called each time the window updates, if something has changed
   //needsUpdate is set to YES elsewhere so we test it here and update as needed
   if(needsUpdate) {
      [self updateDataArray];
      needsUpdate = NO;
   }
}
//*************************table view data source
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
   return [dataArray count];
}

- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
            row:(int)rowIndex
{
   NSString *identifier = [aTableColumn identifier];
   id record = [dataArray objectAtIndex:rowIndex];

   if([identifier isEqualToString:@"isDefaultValue"]) {
      NSMutableArray *defaultPoisonWords = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:SSDefaultPoisonWordListKey]];
      int i = 0;
      while(i < [defaultPoisonWords count]) {
      	//loop through and see if this is a default word
         if([[record poisonWord] isEqualToString:[[defaultPoisonWords objectAtIndex:i] objectForKey:@"word"]] &&
            [[record warningMessage] isEqualToString:[[defaultPoisonWords objectAtIndex:i] objectForKey:@"warning"]]) {
            //this word is a default
            return [NSNumber numberWithBool:YES];
         }

         i++;
      }
      return [NSNumber numberWithBool:NO];
   } else {
      return [record valueForKey:identifier];
   }
}

- (void)tableView:(NSTableView *)aTableView
   setObjectValue:(id)anObject
   forTableColumn:(NSTableColumn *)aTableColumn
              row:(int)rowIndex
{
   NSString *identifier = [aTableColumn identifier];
   id record = [dataArray objectAtIndex:rowIndex];

   if([identifier isEqualToString:@"isDefaultValue"]) {
      if([anObject boolValue]) {
         //we want to add this as a default
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         NSMutableArray *defaultPoisonWords = [NSMutableArray arrayWithArray:[defaults objectForKey:SSDefaultPoisonWordListKey]];
         [defaultPoisonWords addObject:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[record poisonWord], [record warningMessage], nil]
                                                                          forKeys:[NSArray arrayWithObjects:@"word", @"warning", nil]]];
         

         [defaults setObject:defaultPoisonWords forKey:SSDefaultPoisonWordListKey];
      } else {
         //we want to remove this from the defaults
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         NSMutableArray *defaultPoisonWords = [NSMutableArray arrayWithArray:[defaults objectForKey:SSDefaultPoisonWordListKey]];
         int i = 0;
         while(i < [defaultPoisonWords count]) {
            //loop through find and remove this poison word
            if([[record poisonWord] isEqualToString:[[defaultPoisonWords objectAtIndex:i] objectForKey:@"word"]] &&
               [[record warningMessage] isEqualToString:[[defaultPoisonWords objectAtIndex:i] objectForKey:@"warning"]]) {
               //this is the one we want to remove
               [defaultPoisonWords removeObjectAtIndex:i];
               [defaults setObject:defaultPoisonWords forKey:SSDefaultPoisonWordListKey];
               break;
            }

            i++;
         }
      }
   } else {
      [record takeValue:anObject forKey:identifier];
   }
   
}

- (void)tableViewSelectionDidChange:(NSNotification *)note
{
   if([wordTable numberOfRows] > 0) {
      [removeButton setEnabled:YES];
   } else {
      //there's nothing to remove
      [removeButton setEnabled:NO];
   }
}

//*************************

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:wordTable];
   [dataArray release];
   [super dealloc];
}

@end
